package cn.edu.zut.hhr.service.impl;

import cn.edu.zut.hhr.entity.User;
import cn.edu.zut.hhr.mapper.UserMapper;
import cn.edu.zut.hhr.service.UserService;
import cn.edu.zut.hhr.utils.JwtUtil;
import cn.edu.zut.hhr.utils.MD5Util;
import cn.edu.zut.hhr.utils.RedisUtil;
import cn.edu.zut.hhr.vo.IdAndToken;
import cn.edu.zut.hhr.vo.RestResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 标签表 服务实现类
 * </p>
 *
 * @author HHR
 * @since 2021-08-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private RedisUtil redisUtil;

    /**
     * @param user
     * @return
     */
    @Override
    @Transactional
    public RestResponse registerUser(User user) {
        String str = completeUser(user);
        if(str != null && !str.equals("")){
            return RestResponse.fail(str);
        }
        boolean success = userMapper.insertUser(user);
        if (!success){
            return RestResponse.fail("手机号重复");
        }
        Map<String,Object> map = new HashMap<>();
        map.put("username",user.getId());
        String token = JwtUtil.createToken(map);
        redisUtil.setex("username_" + user.getId(),user,60*60*24);
        return RestResponse.success(IdAndToken.getIdAndToken(user.getId(),token));
    }

    @Override
    public boolean updatePasswd(User user, String newPasswd) {
        LambdaUpdateWrapper<User> wrapper = Wrappers.lambdaUpdate();
        wrapper.eq(User::getId,user.getId()).set(User::getPassword,newPasswd);
        int update = userMapper.update(null, wrapper);
        return update==1;
    }

    private String completeUser(User user) {
        if(user.getPassword() == null){
            return "密码不能为空";
        }else {
            user.setPassword(MD5Util.encrypt(user.getPassword()));
        }
        if(user.getPhone() == null){
            return "手机号不能为空";
        }
        if(user.getName() == null){
            return "用户名不能为空";
        }
        /**
         * 只能创建普通用户,admin手动设置
         */
        user.setRole("普通用户");
        if(user.getIntro() == null){
            user.setIntro("这个人很懒什么也没有写");
        }
        if(user.getEmail() == null){
            user.setEmail("");
        }
        user.setSalt("");
        user.setStatus("");
        user.setDel(0);
        /**
         * 默认创建时不上传头像
         */
        user.setImg("");
        return null;
    }
}
